<template>
	<view>
		<block v-for="(item, index) in friendAuthList" :key="index">
			<view class="content" @click="toAddFriendDetaile" :data-friendItem = "item">
				<view class="avatar-l">
					<image :src="item.avatarUrl" mode="widthFix"></image>
				</view>
				<view class="center-wrap">
					<view class="nick-name">{{item.nickName}}</view>
					<view class="auth-msg">对方留言：{{item.authMsg}}</view>
				</view>
				<view class="agree-btns" v-if="item.friendStatus == 2">
					<view class="agree-btn" @click="agreeAddFriend(item)">同意</view>
					<view class="refuse-btn" @click="refuseAddFriend(item)">拒绝</view>
				</view>
				<view class="agree-msg" v-else>
					<view class="agree" v-if="item.friendStatus == 1">已同意</view>
					<view class="refuse" v-else>已拒绝</view>
				</view>
			</view>
		</block>
		<!-- 弹出提示框 -->
		<uni-popup ref="popup" type="center" animation @change="popuChange">
			<view class="popup-content">
				<view class="popup-title">好友备注</view>
				<view class="popu-prompt">
					<input type="text" v-model="friendName" class="friend-name-input"  placeholder="请输入好友备注"/>
				</view>
				<view class="popu-butons">
					<view class="cancel-btn" @click="cancelPopu">取消</view>
					<view class="confirm-btn" @click="confirmPopu">确定</view>
				</view>
			</view>
		</uni-popup>
		<mpopup ref="mpopup" :isdistance="true"></mpopup>
	</view>
</template>

<script>
	import {Friend} from '@/model/friend.js';
	import mpopup from '@/components/xuan-popup/xuan-popup.vue'
	export default {
		data() {
			return {
				friendAuthList:[],
				friendName:"",
				friend:{},
				friendNickname:""
			}
		},
		components:{
			mpopup
		},
		async onLoad() {
			let res = await Friend.findGoodFriendAuthMsgList()
			if(res.code != 200){
				this.$refs.mpopup.open({
					type:'err',
					content:res.message,
					timeout:2000,
					isClick:false
				});
				return
			}
			this.friendAuthList = res.data
			Friend.updateGoodFriendMsgStatus()
		},
		methods: {
			agreeAddFriend(friend){
				this.friendName = friend.nickName
				this.friend = friend
				this.friendNickname = friend.nickName
				this.toggle()
			},
			async refuseAddFriend(friend){
				let res = await Friend.refuseAddFriend({
					id: friend.id
				})
				this.$refs.mpopup.open({
					type:'err',
					content:res.message,
					timeout:2000,
					isClick:false
				});
			},
			toAddFriendDetaile(e){
				let friendItem = e.currentTarget.dataset.friendItem
				// uni.navigateTo({
				// 	url:"../addFriendDetaile/addFriendDetaile",
				// 	success(res) {
				// 		res.eventChannel.emit('acceptDataFromOpenerPage', { friendItem: friendItem })
				// 	}
				// })
			},
			toggle() {
				this.$refs.popup.open()
			},
			hiddenpopup(e){
				this.$refs.popup.showPopup = false
			},
			cancelPopu(){
				this.hiddenpopup()
			},
			popuChange(e){
				console.log(e)
			},
			async confirmPopu(){
				this.hiddenpopup()
				let res = await Friend.agreeAddFriend({
					id:this.friend.id,
					friendUid:this.friend.uid,
					friendName: this.friendName ? this.friendName : this.friendNickname,
					friendNickname:this.friend.nickName
				})
				this.$refs.mpopup.open({
					type:'success',
					content:res.message,
					timeout:2000,
					isClick:false
				});
				setTimeout(()=>{
					uni.navigateBack({
						delta:1
					})
				},1000)
				
			},
		}
	}
</script>

<style>
	page{
		background-color: #F2F2F2;
	}
</style>

<style lang="scss">
	.popup-content{
		background-color: #FFF;
		width: 550rpx;
		box-sizing: border-box;
		border-radius: 20rpx;
		transform: translateY(-25%);
		.popup-title{
			text-align: center;
			height: 100rpx;
			line-height: 100rpx;
			font-size: 32rpx;
			color: #333333;
			font-weight: bold;
			border-bottom: 1rpx #F2F2F2 solid;
		}
		.popu-prompt{
			font-size: 30rpx;
			color: #666666;
			padding: 60rpx 40rpx;
		}
		.popu-butons{
			display: flex;
			justify-content: space-between;
			align-items: center;
			.confirm-btn{
				background-color: $fun-theme-color;
				flex: 1;
				text-align: center;
				height: 80rpx;
				line-height: 80rpx;
				border-bottom-right-radius: 20rpx;
			}
			.cancel-btn{
				flex: 1;
				text-align: center;
				height: 80rpx;
				line-height: 80rpx;
				border-bottom-left-radius: 20rpx;
			}
		}
	}
	.friend-name-input{
		background-color: #F2F2F2;
		border-radius: 10rpx;
		padding: 10rpx;
		height: 60rpx;
		font-size: 26rpx;
	}
	.content{
		background-color: #FFF;
		margin-top: 5rpx;
		padding: 20rpx 30rpx;
		display: flex;
		align-items: center;
		.avatar-l{
			image{
				width: 100rpx;
				height: 100rpx;
				border-radius: 50%;
			}
			margin-right: 20rpx;
		}
		.center-wrap{
			flex: 1;
			.nick-name{
				font-size: 30rpx;
				color: #333333;
				width: 380rpx;
				overflow:hidden; //超出的文本隐藏
				text-overflow:ellipsis; //溢出用省略号显示
				white-space:nowrap; //溢出不换行
			}
			.auth-msg{
				margin-top: 5rpx;
				font-size: 28rpx;
				color: #999999;
				width: 380rpx;
				// overflow:hidden; //超出的文本隐藏
				// text-overflow:ellipsis; //溢出用省略号显示
				// white-space:nowrap; //溢出不换行
			}
		}
		.agree-btns{
			display: flex;
			width: 200rpx;
			justify-content: space-between;
			.agree-btn{
				// color: $fun-theme-color;
				background-color: $fun-theme-color;
				padding: 10rpx 20rpx;
				border-radius: 10rpx;
				font-size: 26rpx;
			}
			.refuse-btn{
				background-color: red;
				color: #FFF;
				padding: 10rpx 20rpx;
				border-radius: 10rpx;
				font-size: 26rpx;
			}
		}
		
		.agree-msg{
			width: 100rpx;
			text-align: center;
			font-size: 26rpx;
		}
	}
</style>
